/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package horde.internalResources.HorrorControl.HCvis;

import horde.internalResources.HorrorControl.HorrorControl;
import horde.internalResources.HorrorControl.HCvis.components.LockedTable;
import horde.internalResources.HorrorControl.HCvis.components.TablePanel;
import horde.internalResources.dictionaries.dictionary;
import horde.internalResources.horrorControl.HCvis.tracesPanel;
import horde.utils.iconFactory.iconFactory;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.DefaultListModel;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.ListSelectionModel;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author Arturo Mazón
 */
public class hcPanel extends JPanel{
     private static hcPanel instance = null;
     private static iconFactory ic = new iconFactory();
     private Dimension d =new Dimension(800,700);
     private JPanel pDebugging,pTraces,pExceptions,pLoader,pStats;
//     private tracesPanel pTraces;
     private JTabbedPane paneles;
     private Dimension tamPanel = new Dimension(400,500);
     protected JList list;
     private JLabel labelListDebug = new JLabel(dictionary.getString("hcVis.labelListDebug"));

     private JPanel tracesPList;

     protected LockedTable tableModel;
    protected TablePanel tracesTable;

     private static HorrorControl hc = null;
     protected MouseListener mouseListener;


     private synchronized static void createInstance() {
        if (instance == null) {
            instance = new hcPanel();
        }
    }

    private synchronized static void createInstance(HorrorControl hc) {
        if (instance == null) {
            instance = new hcPanel(hc);

        }
    }
    public static hcPanel getInstance() {
        if (instance == null) {
            createInstance();
        }
        return instance;
    }


    public static hcPanel getInstance(HorrorControl hc) {
        if (instance == null) {
            createInstance(hc);
        }
        return instance;
    }

      private hcPanel(){
        super();
        hc = HorrorControl.getInstance();
        createElements();
        organizeElements();
        crearMouseListener();
        list.addMouseListener(mouseListener);
        this.setSize(tamPanel);
        this.setVisible(true);
      }

      private hcPanel(HorrorControl hc){
        super();
        this.hc = hc;
        createElements();
        organizeElements();
        crearMouseListener();
        list.addMouseListener(mouseListener);
        this.setSize(tamPanel);
        this.setVisible(true);
      }

    private void createElements() {
        list = new JList();
        pDebugging = new JPanel();

        tracesPList = new JPanel();

        pTraces = new JPanel();
        pExceptions = new JPanel();
        pLoader = new JPanel();
        pStats = new JPanel();

        pDebugging.setSize(d);
        pTraces.setSize(d);
        pExceptions.setSize(d);
        pLoader.setSize(d);
        pStats.setSize(d);

        tracesTable = new TablePanel();
        tracesTable.setTableModel(crearModeloTabla());

        DefaultListModel modelL = listModel();
        list.setModel(modelL);

        list.addListSelectionListener(new ListSelectionListener() {

            public void valueChanged(ListSelectionEvent e) {
                tracesTable.setFilterText(list.getSelectedValue().toString());
                tracesTable.updateFilter();
            }
        });
        tracesPList.setPreferredSize(new Dimension(100,500));
        labelListDebug.setForeground(Color.blue);
        tracesPList.add(labelListDebug);
        tracesPList.add(list);
        pTraces.setLayout(new BorderLayout());
        pTraces.add(tracesPList,BorderLayout.WEST);
        pTraces.add(tracesTable,BorderLayout.CENTER);
        
        paneles = new JTabbedPane();
        pTraces= new tracesPanel();
        paneles.addTab(dictionary.getString("hcVis.traces"), ic.getIcon("iconTraces[32x32].PNG"), pTraces);
        paneles.addTab(dictionary.getString("hcVis.debug"), ic.getIcon("iconDepur[32x32].PNG"), pDebugging);
        paneles.addTab(dictionary.getString("hcVis.exceptions"), ic.getIcon("iconException[32x32].PNG"), pExceptions);
        paneles.addTab(dictionary.getString("hcVis.loader"), ic.getIcon("iconLoader[32x32].PNG"), pLoader);
        paneles.addTab(dictionary.getString("hcVis.Stats"), ic.getIcon("iconStats[32x32].PNG"), pStats);
    }

    private void organizeElements() {
        

        this.setLayout(new BorderLayout());
        this.add(paneles,BorderLayout.CENTER);
    }

     private DefaultListModel listModel() {
         DefaultListModel modelL = new DefaultListModel();
         Object[] agents = hc.getAgents();
         for (int i =0;i< agents.length;i++){
             modelL.addElement(agents[i]);
         }
         return modelL;
     }
//
     public void setJListModel(Object o){
//         pTraces.setJListModel(o);
     }


     private DefaultTableModel crearModeloTabla() {
        String[] columnas = {"Source", "Msg", "Lvl"};
        return new DefaultTableModel(null, columnas);
    }

     public void addTrace(String[] data) {
        DefaultTableModel t = (DefaultTableModel) tracesTable.getTableModel();
        try {
            t.addRow(data);
        } catch (Exception e) {
            // Este metodo a veces genera excepciones, pero se arreglan al actualizarse,
            // asi que aqui mejor no poner nada porque solo lleva a errores
        }
//         pTraces.addTrace(data);
    }

    private void crearMouseListener() {
        mouseListener = new MouseListener() {

            public void mouseClicked(MouseEvent e) {
                // Detecto doble click
                if (e.getClickCount() == 2) {
                    TablePanel panelDeTrazas = new TablePanel();
                    panelDeTrazas.setTableModel(tracesTable.getTableModel());
                    panelDeTrazas.setCurrentFilter(list.getSelectedValue().toString());
                    panelDeTrazas.filterField.setEnabled(false);
                    JFrame f = new JFrame(list.getSelectedValue().toString());
                    f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
                    f.setLayout(new BorderLayout());
                    f.add(panelDeTrazas, BorderLayout.CENTER);
                    f.setVisible(true);
                    f.setSize(400, 300);
                    f.setLocationRelativeTo(null);
                }
            }

            public void mousePressed(MouseEvent e) {
            }

            public void mouseReleased(MouseEvent e) {
            }

            public void mouseEntered(MouseEvent e) {
            }

            public void mouseExited(MouseEvent e) {
            }
        };
    }
    protected class SharedListSelectionHandler implements ListSelectionListener {

        public void valueChanged(ListSelectionEvent e) {
            ListSelectionModel lsm = (ListSelectionModel) e.getSource();
            String s = "";
            if (lsm.isSelectionEmpty()) {
                tracesTable.setFilterText(s);
                tracesTable.updateFilter();
            } else {
                // Find out which indexes are selected.
                int minIndex = lsm.getMinSelectionIndex();
                int maxIndex = lsm.getMaxSelectionIndex();

                for (int i = minIndex; i <= maxIndex; i++) {
                    if (lsm.isSelectedIndex(i)) {
                        s = s + tracesTable.filterField.getText().toString() + " ";
                    }
                    tracesTable.setFilterText(s);
                    tracesTable.updateFilter();
                }
            }
        }
    }

}
